//
//  YSFontViewController.m
//  YSKitDemo
//
//  Created by MOLBASE on 2018/8/14.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSFontViewController.h"

@interface YSFontViewController () <UIAlertViewDelegate>
{
    NSArray *_fontFamilyNames;
    NSString *_sampleText;
}
@end

@implementation YSFontViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"Demo->自定义显示请编辑";

    UIBarButtonItem *composeItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(edit)];
    self.navigationItem.rightBarButtonItem = composeItem;
    
    _sampleText = @"Demo->自定义显示请编辑";
}

- (void)edit {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Input sample text" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self fontFamilyNames].count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self fontNamesInSection:section].count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self fontFamilyNames][section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellId = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    NSString *fontName = [self fontNamesInSection:indexPath.section][indexPath.row];
    cell.detailTextLabel.text = fontName;
    cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1];
    cell.textLabel.font = [UIFont fontWithName:fontName size:20];
    cell.textLabel.text = _sampleText;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

#pragma mark - Font helper
- (NSArray *)fontNamesInSection:(NSInteger)section {
    NSString *familyName = [self fontFamilyNames][section];
    NSArray *fontNames  = [UIFont fontNamesForFamilyName:familyName];
    return fontNames;
}

- (NSArray *)fontFamilyNames {
    if (!_fontFamilyNames) {
        NSArray *familyNames = [UIFont familyNames];
        _fontFamilyNames = [familyNames sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"%@", _fontFamilyNames);
    }
    return _fontFamilyNames;
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 1) {return;}
    NSString *text = [alertView textFieldAtIndex:0].text;
    if (text.length == 0) {return;}
    _sampleText = text;
    [self.tableView reloadData];
}

@end
